package com.weixin.base.dao.impl;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.springframework.jdbc.core.BeanPropertyRowMapper;
import org.springframework.jdbc.core.namedparam.BeanPropertySqlParameterSource;

import cn.org.rapid_framework.page.Page;
import cn.org.rapid_framework.page.PageRequest;

import com.weixin.base.dao.RoleDao;
import com.weixin.common.BaseSpringJdbcDao;
import com.weixin.entity.BgRight;

public class RoleDaoImpl extends BaseSpringJdbcDao<BgRight, Integer> implements RoleDao {

	@Override
	public Class getEntityClass() {
		// TODO Auto-generated method stub
		return BgRight.class;
	}

	
	public void save(BgRight bgright) {
		String sql = "insert into tbBgRight " 
			 + " (sName,sRemark,nIsValid) " 
			 + " values "
			 + " (:sname,:sremark,1)";
		getNamedParameterJdbcTemplate().update(sql, new BeanPropertySqlParameterSource(bgright));
	}
	
	public Page findByPageRequest(PageRequest<Map> pageRequest) {
		Map map = pageRequest.getFilters();
		String sName = (String) map.get("sName");
		String sSQL = "";
		if (!sName.equals("")) {
			sSQL += " AND sName LIKE '" + sName + "%'";
		}
		String order = "order by nRightId desc";
//		if(pageRequest.getSortColumns()!=null)
//			order = " order by "+pageRequest.getSortColumns();
		String sql_count = "SELECT count(1) FROM tbBgRight WHERE 1=1" + sSQL;
		String sql_page = "SELECT * FROM tbBgRight WHERE 1=1" + sSQL + order;		
	
		return pageQuery(sql_page,sql_count,pageRequest,new BeanPropertyRowMapper(getEntityClass()));
	}
	
	public void update(BgRight entity) {
		String sql = "update tbBgRight set "
					+ " sName=:sname,sRemark=:sremark,nIsValid=:nisValid "
					+ " where nRightId=:nrightId";
		getNamedParameterJdbcTemplate().update(sql, new BeanPropertySqlParameterSource(entity));
	}
	
	public void delById(int sId){
		String sql = "delete from tbBgRight where nRightId=?";
		getSimpleJdbcTemplate().update(sql, sId);
	}
	
	public List<Map<String,Object>> selectRoleMPermiss(int nRoleId){
		
		String sSQL="SELECT rt.nRightId,rt.nRW,rt.nNavTreeId,t.sName FROM tbbgrightrelnavtree rt,tbbgnavtree t WHERE t.nNavTreeId = rt.nNavTreeId AND rt.nRightId = ? AND rt.nNavTreeId NOT IN(SELECT tt.nPId FROM tbbgrightrelnavtree rtt,tbbgnavtree tt WHERE tt.nNavTreeId = rtt.nNavTreeId AND rtt.nRightId = ? )";
		List<Map<String,Object>> rtList = getSimpleJdbcTemplate().queryForList(sSQL,nRoleId,nRoleId);
		
		return rtList;
	}
	
	public void updateRoleMPermiss(int nRoleId,String permissRws){
		
		String[] permissRw = permissRws.split(",");
		for(int i=0;i<permissRw.length;i++){
			String prw = permissRw[i];
			if(!prw.equals("")){
				String[] sTr = prw.split("@");
				int nTreeId = Integer.parseInt(sTr[0]);
				int nRW = Integer.parseInt(sTr[1]);
				String sSQL = "UPDATE tbbgrightrelnavtree SET nRW=? WHERE nRightId=? AND nNavTreeId=?";
				getSimpleJdbcTemplate().update(sSQL,new Object[]{nRW,nRoleId,nTreeId});
			}
		}
	}

	public Map<String,Object> selRolePermiss(int nRoleId){
		Map<String,Object> map = new HashMap<String,Object>();
		String sSQL = "SELECT * FROM tbBgNavTree ORDER BY nNavTreeId";
		List<Map<String,Object>> treeList = getSimpleJdbcTemplate().queryForList(sSQL);
		sSQL = "SELECT * FROM tbBgRightRelNavTree WHERE nRightId=?";
		List<Map<String,Object>> brList = getSimpleJdbcTemplate().queryForList(sSQL,nRoleId);
		map.put("ary_all", treeList);
		map.put("ary_role", brList);
		return map;
	}
	
	public void upDelRolePermiss(int nRoleId,String nNavTrees){
		String sSQL="";
			sSQL = "DELETE FROM tbBgRightRelNavTree WHERE nRightId=?";
			getSimpleJdbcTemplate().update(sSQL, nRoleId);
				String[] nNavTreesArr = nNavTrees.split(",");
				for(int i=0,nLen=nNavTreesArr.length;i<nLen;i++){
					int nNavTree = Integer.parseInt(nNavTreesArr[i]);
//					if(!nNavTree.isEmpty()){
//						String[] sTr = nNavTree.split("@");
//						int nTreeId = Integer.parseInt(sTr[0]);
//						int nRW = Integer.parseInt(sTr[1]);
						sSQL = "INSERT INTO tbBgRightRelNavTree(nRightId,nNavTreeId,nRW) VALUES(?,?,1)";
						getSimpleJdbcTemplate().update(sSQL,new Object[]{nRoleId,nNavTree});
//					}
				}
	}
}